package com.lepu.app.fun.mother_circle.adapter;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.PubContentDetailActivity;
import com.lepu.app.fun.my.bean.BeanPubContent;

public class AdapterMotherPost extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private BaseFragmentActivity mContext = null;
	private ArrayList<BeanPubContent> mArrayList = null;

	public AdapterMotherPost(BaseFragmentActivity context,
			ArrayList<BeanPubContent> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.mother_post_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView.findViewById(R.id.mainItemLayout);
			CircularImageView iconImageView = (CircularImageView) convertView.findViewById(R.id.iconImageView);
			TextView userNameTextView = (TextView) convertView.findViewById(R.id.userNameTextView);
			TextView typeTextView = (TextView) convertView.findViewById(R.id.typeTextView);
			TextView pubTimeTextView = (TextView) convertView.findViewById(R.id.pubTimeTextView);
			TextView commentCountTextView = (TextView) convertView.findViewById(R.id.commentCountTextView);
			TextView lookCountTextView = (TextView) convertView.findViewById(R.id.lookCountTextView);
			
			TextView titleTextView = (TextView) convertView.findViewById(R.id.titleTextView);
			TextView contentTextView = (TextView) convertView.findViewById(R.id.cotentTextView);
			ImageView goodImageView = (ImageView) convertView.findViewById(R.id.topic_item_good_icon);
			ImageView imgImageView = (ImageView) convertView.findViewById(R.id.topic_item_img_icon);
			
			
			holder.mainItemLayout = mainItemLayout;
			holder.iconImageView = iconImageView;
			
			holder.userNameTextView = userNameTextView;
			holder.typeTextView = typeTextView;
			holder.lookCountTextView = lookCountTextView;
			holder.commentCountTextView = commentCountTextView;
			holder.pubTimeTextView = pubTimeTextView;
			
			holder.titleTextView = titleTextView;
			holder.contentTextView = contentTextView;
			holder.goodImageView = goodImageView;
			holder.imgImageView = imgImageView;
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.mainItemLayout.setTag(position);

		final BeanPubContent item = mArrayList.get(position);

		String imageUrl = Setting.getImageUrl(item.UserAvatar);
		//UniversalImageLoadTool.disPlay(imageUrl, holder.iconImageView, R.drawable.app_ic_default_avatar_64);
		LoginUtil.loadSelfUserImage(mContext, holder.iconImageView, imageUrl);

	    holder.userNameTextView.setText(item.UserNickName);
	    holder.typeTextView.setText(item.CategoryTitle);
	    holder.pubTimeTextView.setText(DateUtilBase.friendly_time(item.LatestReplyTime));
	    holder.lookCountTextView.setText("  "+item.ViewCount);
		holder.commentCountTextView.setText("  "+item.ReplyCount);
		
		 if(item.IsGood == 1){
			 holder.goodImageView.setVisibility(View.VISIBLE);
			 if(item.MediaList != null && item.MediaList.size()>0){
				 holder.imgImageView.setVisibility(View.VISIBLE);
				 holder.titleTextView.setText("                "+item.TopicTitle);
			 }else{
				 holder.imgImageView.setVisibility(View.GONE);
				 holder.titleTextView.setText("           "+item.TopicTitle);
			 }
         	
 		}else{
 			holder.goodImageView.setVisibility(View.GONE);
 			 if(item.MediaList != null && item.MediaList.size()>0){
				 holder.imgImageView.setVisibility(View.VISIBLE);
				 holder.titleTextView.setText("           "+item.TopicTitle);
			 }else{
				 holder.imgImageView.setVisibility(View.GONE);
				 holder.titleTextView.setText("      "+item.TopicTitle);
			 }
 		}
		
		//多行自动截断显示，不起作用，系统bug
		UIHelper.setTextViewLines(holder.titleTextView, 2);
		
		holder.contentTextView.setText(item.TopicDetail);
		UIHelper.setTextViewLines(holder.contentTextView, 2);

		holder.iconImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				LoginUtil.jumpUserInfo((BaseFragmentActivity)mContext, item.UserID);
			}
		});

		
		holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,PubContentDetailActivity.class);
				intent.putExtra("tag", 1);
				intent.putExtra("TopicId", item.TopicID);
				
				if(item.IsGood == 1){ //精华
					intent.putExtra("nameType", 2);
				}
				
				mContext.startActivity(intent, true);
			}
		});

		return convertView;
	}

	private static class ViewHolder {

		LinearLayout mainItemLayout;
		CircularImageView iconImageView;
		TextView titleTextView;
		TextView contentTextView;
		
		TextView userNameTextView;
		TextView typeTextView;
		TextView lookCountTextView;
		TextView commentCountTextView;
		TextView pubTimeTextView;
		
		ImageView goodImageView;
		ImageView imgImageView;
		
	}
}
